/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.core.bridge
{
	import org.etherframework.IEther;
	import org.etherframework.core.common.Ether;
	import org.etherframework.core.common.EtherTypes;
	import org.etherframework.core.common.IArgumentFactory;
	import org.etherframework.core.common.IInstanceFactory;
	import org.etherframework.core.common.IObjectProcessor;
	import org.etherframework.core.common.ether_internal;
	import org.etherframework.core.reflection.Parameter;
	import org.etherframework.core.utils.ClassUtils;

	[ExcludeClass]
	public final class BridgeProcessor
	{
		private var _instanceFactory:IInstanceFactory;
		private var _processors:Vector.<IObjectProcessor>;
		private var _facade:IBridgeFacades;
		private var _processorsLen:uint;
		
		public function BridgeProcessor(facade:IBridgeFacades)
		{
			_facade = facade;
		}
		
		public function initialize(instanceFactory:IInstanceFactory, 
								   processors:Vector.<IObjectProcessor>):void
		{
			if (!instanceFactory)
				throw new ArgumentError("Argument instanceFactory:IInstanceFactory can not be null.");
			
			_instanceFactory = instanceFactory;
			_processors = processors;
			_processorsLen = _processors.length;
		}
		
		public function processInstance(target:Object, ether:IEther):Object
		{
			const e:Ether = ether as Ether;
			
			e.ether_internal::__init(target);
			
			if (_processors)
			{
				for (var i:int = 0; i < _processorsLen; i++) 
				{
					_processors[i].process(target, ether, _facade);
				}
			}
			
			e.ether_internal::processed();
			
			return target;
		}
		
		public function processClass(type:Class, ether:IEther, argumentFactory:IArgumentFactory):Object
		{
			var instance:Object;
			var parameter:Parameter;
			var args:Array = null;
			var i:int
			
			const constructorArgs:Vector.<Parameter> = ClassUtils.getConstructorArgs(type);
			
			if (_processors)
			{
				for (i = 0; i < _processorsLen; i++) 
				{
					_processors[i].process(type, ether, _facade);
				}
			}
			
			if (constructorArgs)
			{
				for (i = 0; i < constructorArgs.length; ++i)
				{
					parameter = constructorArgs[i];
					
					if (EtherTypes.isUnsupported(parameter.type as Class))
						throw new Error("Object " + type + " can not be instantiated. " +
							"Argument with type " + parameter.type + " is not supported.");
					
					instance = (parameter.type == IEther) ? ether : 
						argumentFactory.createArgument(parameter, ether.context.copy());
					
					if (!instance)
						throw new Error("Argument  " + parameter.type + " for constructor " + 
							type + " could not be found.");
					
					if (!args)
						args = [];
					
					args[i] = instance;
				}
			}
			
			return processInstance(_instanceFactory.createInstance(type, args, ether), ether);
		}
	}
}